<?php
session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );
$key = WB_AKEY;
$o = new WeiboOAuth( WB_AKEY , WB_SKEY , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );
dump($o);
$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;
dump($last_key);
$token = $last_key['oauth_token'].'_'.$last_key['oauth_token_secret'];
$userId = $last_key['user_id'];
$c = new WeiboClient( WB_AKEY , WB_SKEY ,$last_key['oauth_token'],$last_key['oauth_token_secret']);
$me = $c->verify_credentials();
$userName = $me['name'];
echo $userName;
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME,$con);
mysql_query("SET NAMES utf8");
$sql0="select * from cai_appkey where type='v1' and key_key = '".$key."' and name = '".$programe."'"; 
$result0 = mysql_query($sql0);
while($row=mysql_fetch_array($result0)){
	$sns_id = $row['id'];
    $name = $row['name'];
}
echo '来自:'.$name;

if($userId){
    $sql="select * from cai_uid where sns='weibo' and uid='".trim($userId)."' and sns_id = '".$sns_id."'"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    if (!mysql_num_rows($result)){
        $sql2="INSERT INTO `cai_uid` (`id`, `uid`, `token` , `name`,`sns`,`expires`,`sns_id`) VALUES (NULL,'".trim($userId)."','".$token."','".$userName."','weibo','0','".$sns_id."');";
        if(mysql_query($sql2)){
            echo "插入成功";
        }else{
            echo "插入成功";
        } 
    }else{
        $sql3="UPDATE `cai_uid` SET `token` =  '".$token."' ,`name` =  '".$userName."',`expires` =  '0' WHERE  uid='".$userName."' and sns= 'weibo' and sns_id= '".$sns_id."';";
        if(mysql_query($sql3)){
            echo "更新成功";
        }else{
            echo "更新失败";
        } 
    }
}

?>
授权完成,<a href="../shouquan.php">查看所有授权</a>
